iT邦幫忙

2024 iThome 鐵人賽

DAY 1
0
DevOps

將 AI Code Review 整進 CICD系列 第 1

將 AI Code Review 整進 CICD Day1

  • 分享至 

  • xImage
  •  

在 DevOpsDay 的洗禮後,發現很多人已經開始試著將 AI 整進工作流程中做出 wakuwaku 的案例。但自己還停留在跟 Claude Debug 跟伸手要 code 的階段,於是乎決定來試著摸索將 AI 的應用整進 CICD 中。

一開始定義需求希望是可以有一個 AI Senior 工程師協助我做 Code Review,開始做調研有幾種

  • Azure DevOps Pipeline,Azure 內建,但是跟 Azure 不熟,所以跳過

  • Code Intelligence CI Spark vs Codium-ai/pr-agent,工程師的判斷標準很簡單,誰 github 追蹤數多用誰的。而且 Codium-ai/pr-agent 的 Doc 看下來寫得比較完整

Codium-ai 是美國創業公司開發的人工智能工具,旨在幫助開發人員更快地編寫測試並在部署前發現錯誤。 產品特點包含: AI 驅動的分析:分析源代碼、文檔字符串和註釋以生成有意義的測試。最有名的應該是他的一鍵生成單元測試。

我們先從大架構來看他是如何應用的,在思考接下來要怎麼實作。

CodiumAI PR-Agent is an open-source tool to help efficiently review and handle pull requests.

  • 開源的,所以可以拿來玩

  • review and handle pull requests → 觸發的時機

從 example results 快速了解他可以做到的功能,包含

  • describe - 快速總結這次 PR 重點

  • review - 提醒 review 的重點

  • improve - 給予改進建議

  • generate_labels - 產出分數 label or 預期動作 → 未來應該可以整進 code review 過不過關或績效評估

其他更多應用:https://pr-agent-docs.codium.ai/tools/

接下來就是重點了,要如何使用呢?它分成 Installation GuideUsage Guide

我們一步步拆解,從 Installation Guide 可以得知他有自架(self-hosted) 跟官方付錢版本(PR-Agent Pro)。 小資族乖乖選擇自架。平台有 local 跟 github/gitlab/azure。因為公司在用 GitLab,所以會拿 GitLab 練手。

前置作業我們看到要有 OpenAI key 跟平台的 personal access token with the repo scope。OpenAI API 沒有 Free usage,看樣子要花錢錢 :<

  1. 先建立 repo main-feature (不用 group,因為 group access token 要 Premium license)

  2. 直接從 console 上建立 branch,並進行 code 改動,再建立 MR,得到 merge request url : https://gitlab.com/ooii8929/main-feature/-/merge_requests/2

  3. 建立 personal access token(權限給 api, read_registry)

一、安裝程式

來看看如何安裝,詳細參考官網。我們會先用 locally 的練練手,在 pip 跟 docker 間,熟悉的 Docker 最對味

  1. Clone this repository

    git clone https://github.com/Codium-ai/pr-agent.git
    cd /pr-agent
    
  2. 我們要將一開始提到的 OpenAI key 和 gitlab user token 放進 secrets.toml 檔裡面,所以先從 template copy 一份下來

    cp pr_agent/settings/.secrets_template.toml pr_agent/settings/.secrets.toml
    chmod 600 pr_agent/settings/.secrets.toml
    # Edit .secrets.toml file
    
  3. 在 pr_agent/settings/.secrets.toml 找到 gitlab 欄位

    [gitlab]
    # Gitlab personal access token
    personal_access_token = "glpat-xxxx"
    
    [openai]
    key = "sk-xxxxx"  # Acquire through https://platform.openai.com
    
  4. pr_agent/settings/configuration.toml 要調整

    [config]
    # models
    model="gpt-4-turbo-2024-04-09"
    model_turbo="gpt-4o"
    fallback_models=["gpt-4-0125-preview"]
    # CLI
    git_provider="gitlab" -> 調整成 gitlab
    

    如果configuration.toml 沒調整,會遇到 provider error

    File "/Users/alvinlin/Desktop/Personal/side-project/pr-agent/pr_agent/git_providers/__init__.py", line 62, in get_git_provider_with_context
        raise ValueError(f"Failed to get git provider for {pr_url}") from e
    ValueError: Failed to get git provider for https://gitlab.com/ooii8929/main-feature/-/merge_requests/2
    

二、運行程式

  1. 運行看看

    python -m venv venv 
    source ./venv/bin/activate
    python3 -m pr_agent.cli --pr_url https://gitlab.com/ooii8929/main-feature/-/merge_requests/2 review
    
  2. 完成訊息

    python3 -m pr_agent.cli --pr_url https://gitlab.com/ooii8929/main-feature/-/merge_requests/2 review
    2024-08-01 21:49:44.797 | INFO     | pr_agent.git_providers.git_provider:get_main_pr_language:266 - No languages detected
    2024-08-01 21:49:49.945 | INFO     | pr_agent.tools.pr_reviewer:run:107 - Reviewing PR: https://gitlab.com/ooii8929/main-feature/-/merge_requests/2 ...
    2024-08-01 21:49:52.092 | INFO     | pr_agent.algo.pr_processing:get_pr_diff:61 - PR main language: Other
    2024-08-01 21:49:52.098 | INFO     | pr_agent.algo.pr_processing:get_pr_diff:71 - Tokens: 2237, total tokens under limit: 32000, returning full diff.
    2024-08-01 21:50:08.078 | INFO     | pr_agent.tools.pr_reviewer:set_review_labels:408 - Setting review labels:
    ['Review effort [1-5]: 3']
    

https://ithelp.ithome.com.tw/upload/images/20240815/20118525yLeoquYM36.png

  1. 如果再部署一次會直接更新原本的 commit

    python3 -m pr_agent.cli --pr_url https://gitlab.com/ooii8929/main-feature/-/merge_requests/2 review
    2024-08-01 21:56:50.387 | INFO     | pr_agent.git_providers.git_provider:get_main_pr_language:266 - No languages detected
    2024-08-01 21:56:55.313 | INFO     | pr_agent.tools.pr_reviewer:run:107 - Reviewing PR: https://gitlab.com/ooii8929/main-feature/-/merge_requests/2 ...
    2024-08-01 21:56:56.879 | INFO     | pr_agent.algo.pr_processing:get_pr_diff:61 - PR main language: Other
    2024-08-01 21:56:56.884 | INFO     | pr_agent.algo.pr_processing:get_pr_diff:71 - Tokens: 2237, total tokens under limit: 32000, returning full diff.
    2024-08-01 21:57:05.540 | INFO     | pr_agent.tools.pr_reviewer:set_review_labels:411 - Review labels are already set:
    ['Review effort [1-5]: 3']
    2024-08-01 21:57:07.327 | INFO     | pr_agent.git_providers.git_provider:publish_persistent_comment_full:171 - Persistent mode - updating comment https://gitlab.com/ooii8929/main-feature/-/merge_requests/2#note_2026326099 to latest review message
    

https://ithelp.ithome.com.tw/upload/images/20240815/20118525p5onqgqumn.png


下一篇
將 AI Code Review 整進 CICD Day2
系列文
將 AI Code Review 整進 CICD24
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言